<template>
  <BaseModal title="对内转入" :visible="visible" @cancel="handleCancel" @ok="handleOk" width="800px" destroyOnClose>
    <custom-form
      ref="ruleForm"
      :isValidate="true"
      :editModel="''"
      :rules="rules"
      :form="formInfo"
      :formList="formItemList"
      @selectNode="handleSelectOrg"
    ></custom-form>
  </BaseModal>
</template>

<script>
import BaseModal from '@/components/BaseModal'
import PartyTreeSelecgt from '../../componments/partyTreeSelect.vue'
import { packageConfirm } from '@/utils/baseAction.js'
import { turnInMember } from '@/api/partyMasses/partyManage/partyMember.js'

export default {
  name:'a'+ Date.now(),
  components: {
    BaseModal,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    initData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      formInfo: {},
      rules: {
        partyOrgId: [{ required: true, message: '请选择', trigger: 'change' }],
      },
    }
  },
  computed: {
    formItemList() {
      return [
        {
          label: '党员名称',
          keyName: 'partyMemberName',
          disabled: true,
          type: 'BaseInput',
          span: 12,
        },
        {
          label: '转入党支部',
          keyName: 'partyOrgId',
          type: PartyTreeSelecgt,
          span: 12,
          attrs: {
            onlySelectTypes: [ 3 ],
            orgStatus: 1,
          }
        },
        {
          label: '备注说明',
          keyName: 'remark',
          type: 'BaseTextarea',
          span: 24,
        },
      ]
    },
  },
  watch: {
    initData: {
      immediate: true,
      deep: true,
      handler(val) {
        this.formInfo = { ...val, partyOrgId: void 0 } || {}
      },
    },
  },
  methods: {
    handleSelectOrg(key, data) {
      const { id } = data
      this.formInfo = {
        ...this.formInfo,
        partyOrgId: id,
      }
      this.$nextTick(() => {
        this.$refs.ruleForm.validateField('partyOrgId')
      })
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          this.$message.warning("请填写完所有必填项信息")
          return
        }
        packageConfirm({
          content: '请确认是否进行对内转入？',
          callback: async () => {
            const params = {
              ...this.formInfo,
              flowId: this.formInfo.id,
            }
            await turnInMember(params)
            this.$message.success('转入成功')
            this.$emit('ok')
          },
        })
      })
    },
    handleCancel() {
      this.$emit('update:visible')
    },
  },
}
</script>

<style>
</style>
